package com.sltx.controller;

import com.sltx.controller.base.BaseController;
import com.sltx.util.Conversion;
import com.sltx.util.PageData;
import com.sltx.util.TextUtils;
import net.sf.json.JSONObject;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping(value="/jzzs")
public class jzzsController extends BaseController {

	/**
	 *
	 */
	private static final long serialVersionUID = 8900023230688749794L;

//	@Autowired
//	private JzzsService jzzsService;


	/**
	 * 企业名单检索
	 */
	@RequestMapping(value="/getEntByKeyword",produces="application/json;charset=UTF-8")
	@ResponseBody
	public Object getEntByKeyword(HttpServletRequest request){
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = this.getPageData();
		pd.put("keyword", Conversion.codeConversion(pd.get("keyword").toString()));
		pd.put("type", Conversion.codeConversion(pd.get("type").toString()));
		logBefore(logger,"企业名片检索接口 信息 param {keyword:"+pd.get("keyword").toString()+",type:"+pd.get("type").toString()+"}");
		BigDecimal begin = BigDecimal.valueOf(System.currentTimeMillis());
		JSONObject jo = Conversion.getServiceData("getEntByKeyword", pd);
		List<Map<String,Object>> list = jo.getJSONArray("RESULTDATA");
		Map<String,Object> pageInfo = (Map<String, Object>) jo.get("PAGEINFO");
		BigDecimal time = BigDecimal.valueOf(System.currentTimeMillis()).subtract(begin).divide(BigDecimal.valueOf(1000));
		String usedTime = TextUtils.format("{0,number,#.###}", time);
		map.put("getRiskStormEventInfo", list);
		map.put("pageInfo", pageInfo);
		map.put("getRiskStormEventInfoTime", usedTime);
		Object cb = request.getParameter("callback");
		String result = Conversion.convertJsonString(null,map,cb);
		System.out.println(result);
		return result;
	}
	/**
	 * 企业基本信息
	 */
	@RequestMapping(value="/getRegisterInfo",produces= MediaType.APPLICATION_JSON_VALUE+";charset=utf-8")
	@ResponseBody
	public Object getRegisterInfo(HttpServletRequest request){
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		PageData pd = this.getPageData();
		String entName = Conversion.codeConversion(pd.get("entName").toString());
		pd.put("entName", entName);
		Object cb = request.getParameter("callback");
		String result = Conversion.convertJsonString(list,null,cb);
		return result;
	}

	//桥接接口
	public static Object interfaceDemo(String path, String data) {
		String result = "";

		try {
			URL url = new URL(path);
			HttpURLConnection conn = (HttpURLConnection)url.openConnection();
			PrintWriter out = null;
			conn.setRequestProperty("accept", "*/*");
			conn.setRequestProperty("connection", "Keep-Alive");
			conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
			conn.setDoOutput(true);
			conn.setDoInput(true);
			out = new PrintWriter(conn.getOutputStream());
			out.print(data);
			out.flush();
			InputStream is = conn.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is));

			String str;
			while((str = br.readLine()) != null) {
				result = result + str;
				System.out.println(result);
			}

			is.close();
			conn.disconnect();
		} catch (Exception var9) {
			var9.printStackTrace();
		}

		return result;
	}

	/**
	 * 企业名单检索
	 */
	//@RequestMapping(value="/getEntByKeyword",produces="application/json;charset=UTF-8")
	@ResponseBody
	public Object getEntByKeyword(@RequestParam(required = false) String type, @RequestParam(required = false) String keyword){
		String data = "type=" + type + "&keyword=" + keyword;//192.202.58.9
		return interfaceDemo("http://192.202.58.9:18052/sjs_product/jzzs/getEntByKeyword?",data);
	}


}
